package com.ruoyi.web.controller.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.estate.Resident;
import com.ruoyi.system.mapper.estate.EstateResidentMapper;
import com.ruoyi.system.service.estate.IResidentService;

/**
 * 住户信息完善控制器
 */
@RestController
@RequestMapping("/api/resident/profile")
public class ResidentProfileController extends BaseController {
    
    @Autowired
    private IResidentService residentService;
    
    @Autowired
    private EstateResidentMapper residentMapper;
    
    /**
     * 检查当前用户是否已关联住户信息
     */
    @GetMapping("/check")
    public AjaxResult checkResidentProfile() {
        Long userId = SecurityUtils.getUserId();
        Resident resident = residentMapper.selectResidentByUserId(userId);
        
        return AjaxResult.success()
                .put("hasProfile", resident != null)
                .put("resident", resident);
    }
    
    /**
     * 完善住户信息
     */
    @PostMapping("/complete")
    public AjaxResult completeProfile(@RequestBody Resident resident) {
        // 设置当前用户ID
        resident.setUserId(SecurityUtils.getUserId());
        
        // 检查是否已存在关联
        Resident existingResident = residentMapper.selectResidentByUserId(SecurityUtils.getUserId());
        if (existingResident != null) {
            // 更新现有住户信息
            resident.setResidentId(existingResident.getResidentId());
            return toAjax(residentService.updateResident(resident));
        } else {
            // 新增住户信息
            return toAjax(residentService.insertResident(resident));
        }
    }
} 